package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/6 19:25
 * https://leetcode-cn.com/problems/search-insert-position/
 */
public class Title35 {

    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (nums[middle] == target) {
                return middle;
            } else if (nums[middle] > target) {
                right = middle - 1;
            } else {
                left = middle + 1;
            }
        }
        //left > right
        return left;
    }


    /**
     * 有序数组
     * @param nums
     * @param target
     * @return
     */
    public int searchInsert1(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int middle = left + (right - left) / 2;
            if (nums[middle] < target) {
                left = middle + 1;
            }else if (nums[middle] > target){
                right = middle - 1;
            }else{
                return middle;
            }
        }
        return left;
    }


    public static void main(String[] args) {

    }
}
